package com.nyist.config;

import java.net.InetAddress;

import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

@Configuration
@PropertySource(value = "classpath:config/elasticsearch.properties")
public class ESConfig {

	private static final Logger logger = LoggerFactory.getLogger(ESConfig.class);
	
	@Value("${es.hostName}")
	private String hostName;
	
	@Value("${es.transport}")
	private Integer transport;
	
	@Value("${es.cluster.name}")
	private String clusterName;
	
	@Bean
	public TransportClient transClient( ) {
		logger.info("elastic开始初始化......");
		
		TransportClient transportClient = null;
		try {
			TransportAddress address = 
					new TransportAddress(InetAddress.getByName(hostName), transport);
			//配置信息
			Settings es = Settings.builder().put("cluster.name", clusterName).build();
			transportClient = new PreBuiltTransportClient(es);
			transportClient.addTransportAddress(address);
		} catch (Exception e) {
			logger.error("elastic创建失败", e);
		}
		return transportClient;
	}
	
}
